<?php

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

class LogsController extends AppController{
	
	/*****************
	** Variables Generales
	*****************/
	public $name = 'Logs';//nombre controlador
	public $uses = array('Log');//modelos empleados
	public $layout = 'admin';//disposición de diseño por defecto

	/*****************
	** Variables Administracion
	*****************/
	public $search_form = false;//mostramos el buscador si o no
	public $sub_menu = array("logs" => "index", "config" => "index");//Subsecciones en submenu
	public $action_menu = null;//Acciones del menu accion
	public $lnregpp = array(20,40,60,80,100);//Numero de registros mostrados por pagina

	/*****************
	** Respuestas de controlador a una accion
	*****************/
	public $result_action;//Resultado de la operaciones
	public $msg_result;//Mensaje de la operacion

////////////////////////////////////////////////////////////////////////////
//////*ESTOS METODOS DEBEN ESTAR LIMITADOS A LOS ADMINISTRADORES*//////////
//////////////////////////////////////////////////////////////////////////


	/**
	 * Listado de logs del sistema en el panel de administración
	 *
	 * @return void
	 */
	public function admin_index(){

		//*RECOJEMOS PARAMETROS ENVIADOS*/
		//numero de registros a mostrar
		if(isset($this->params['named']['nreg']) && in_array($this->params['named']['nreg'],$this->lnregpp)){
			$i_array = array_keys($this->lnregpp,$this->params['named']['nreg']);
			$nreg = $this->lnregpp[$i_array[0]];
		}else{
			$nreg = $this->lnregpp[0];
		}

		//Parametros que paso al paginador
		$this->paginate = array('limit' => $nreg,
								'order' => null,
								'conditions' => null,
								'recursive' => -1);

		/*PASAMOS PARAMETROS A LA VISTA*/
		//obtengo todos los registros y setemos
		$this->set('registers', $this->paginate('Log'));

		//submenu
		$this->set('sub_menu', $this->sub_menu);

		//Mostramos el buscador
		$this->set('search_form', $this->search_form);	


		//lista de valores para mostrar por pantalla
		$this->set('lnregpp', $this->lnregpp);

		//Valor elegido en la lista de valores
		$this->set('nreg', $nreg);
	}


	/**
	 * Marca el Log como leido y nos lleva a la acción indicada en el registro
	 *
	 * @return void
	 */
	public function admin_view($id){

		$this->autoRender = false;

		if(!empty($id)){

			//Coloco como leido
			$this->Log->setRead($id,'1');
		
			//Obtenemos el valor de la URL
			$this->Log->id = $id;
			$register_Log = $this->Log->read();

			//Redireccion
			$this->redirect($register_Log['Log']['url']);
		}else{
			//Redireccion 
			$this->redirect($this->referer());
		}
		  
	}


	/**
	 * Elimina registros de la BD
	 *
	 * @return void
	 */
	public function admin_delete(){
		
		$this->autoRender = false;

		if($this->Log->deleteAll($conditions = array('1' => '1'), $cascade = true, $callbacks = false)){
			//Mensaje a lanzar
			$this->result_action = true;
			$this->msg_result = "del_registers_correctly";
		}else{
			//Mensaje a lanzar
			$this->result_action = false;
			$this->msg_result = "no_operations";
		}

		//Mensaje que devuelve
		$this->Session->setFlash(__($this->msg_result, true), 'default');
		//Redireccion 
		$this->redirect($this->referer());
	}

}

?>
